package com.copm.ifm.servers.sys.controller;


import com.copm.ifm.base.api.pojo.PageDTO;
import com.copm.ifm.base.api.pojo.PageVO;
import com.copm.ifm.base.basic.ServiceResponse;
import com.copm.ifm.base.basic.constant.DbConstants;
import com.copm.ifm.base.basic.pojo.vo.BaseTreeVO;
import com.copm.ifm.base.service.base.BaseController;
import com.copm.ifm.servers.sys.dto.SysMenusCustomDTO;
import com.copm.ifm.servers.sys.entity.SysMenusCustom;
import com.copm.ifm.servers.sys.service.SysMenusCustomService;
import com.copm.ifm.servers.sys.vo.SysMenusCustomVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单自定义表相关接口类（自定义菜单的添加和设置）
 * *
 * * @author tp
 * * @date 2020/12/21 15:18
 */
@Slf4j
@Api(tags = "菜单自定义/sys-menus-custom")
@RestController
@RequestMapping("/sys-menus-custom")
public class SysMenusCustomController extends BaseController {

    @Autowired
    SysMenusCustomService sysMenusCustomService;

    @ApiOperation(value = "树状图-获取自定义菜单树状图")
    @GetMapping("/tree")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "是否只查启用 [0全查 1只查启用]，默认查全部",name = "enabled"),
            @ApiImplicitParam(value = "查询条件，搜索组织树的条件",name = "equName")})
    public ServiceResponse<? extends BaseTreeVO> tree(@RequestParam(defaultValue = "0") int enabled,
                                                      @RequestParam String equName) {
        return success(sysMenusCustomService.getEquTypeTree(enabled, equName));
    }

    @ApiOperation("点击新增一级分类/新增下级分类")
    @GetMapping("/click/add/menus")
    public ServiceResponse<SysMenusCustomVO> clickAddEqu(@ApiParam("父级ID，为空时则增加的是一级分项，非空时将在该ID下新增分项")
                                                         @RequestParam Integer parentEquId) {
        SysMenusCustomVO sysMenusCustomVO = sysMenusCustomService.clickAddEqu(parentEquId);
        return success(sysMenusCustomVO);
    }

    @ApiOperation("添加-菜单自定义表")
    @PostMapping("/add")
    public ServiceResponse<SysMenusCustom> add(@ApiParam("添加菜单自定义单对象")
                                               @RequestBody SysMenusCustomDTO addDTO) throws Exception {
        //处理父路径参数值，当父值不为空，或者删除头尾为空的时候；
        if (addDTO.getParentPath() != null && ("").equals(addDTO.getParentPath().trim())) {
            addDTO.setParentPath(null);
        }
        return success(sysMenusCustomService.add(addDTO));
    }

    @ApiOperation("修改-菜单自定义表分类")
    @PutMapping("/modify")
    public ServiceResponse<SysMenusCustom> modify(@ApiParam("菜单自定义实体")
                                                  @RequestBody SysMenusCustomDTO entity) throws Exception {
        return success(sysMenusCustomService.modify(entity));
    }

    @ApiOperation("查询-根据by id")
    @GetMapping("/by/id")
    public ServiceResponse<?> getById(@ApiParam(value = "主键id", required = true)
                                      @RequestParam int id) {
        SysMenusCustomVO sysMenusCustomVO = sysMenusCustomService.selectById(id);
        if (StringUtils.isEmpty(sysMenusCustomVO)) {
            return fail("未查到设备设施分类详情");
        }
        return success(sysMenusCustomVO);
    }

    @ApiOperation("删除-by id")
    @DeleteMapping("/by/id")
    public ServiceResponse<?> deleteById(@ApiParam(value = "主键id", required = true) @RequestParam int id) {
        //修改delete字段的值为1
        sysMenusCustomService.updateDelete(id);
        return success();
    }

    @ApiOperation("删除父节点-根据id")
    @DeleteMapping("/parent/by/id")
    public ServiceResponse<?> deleteParentById(@RequestParam int id) {
        //修改delete字段的值为1
        sysMenusCustomService.updateParentDelete(id);
        return success();
    }

    /**
     * 点击权限刷新按钮
     */
    @ApiOperation("点击权限刷新按钮")
    @GetMapping("/menu/refresh")
    public ServiceResponse<?> menuAuthRefresh() {
        int i = sysMenusCustomService.menuAuthRefresh();
        if (i == DbConstants.REFRESH_RE) {
            return success("刷新失败，已是最新操作权限");
        }
        if (i == DbConstants.REFRESH_DONE) {
            return success("刷新权限表成功");
        }
        return success();
    }

    @ApiOperation("添加系统预设应用分页")
    @PostMapping("/page/by/name")
    public ServiceResponse<PageVO<SysMenusCustomVO>> page(@RequestBody PageDTO<SysMenusCustomDTO> param) {
        PageVO<SysMenusCustomVO> result = sysMenusCustomService.menuListByName(param);
        return success(result);
    }

    @ApiOperation("批量添加菜单自定义表")
    @PostMapping("/add/batch")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "批量添加菜单自定义单对象",name = "addDTOList"),
            @ApiImplicitParam(value = "添加的父级路径",name = "parentPath")})
    public ServiceResponse<?> addBatch(@RequestBody List<SysMenusCustomDTO> addDTOList,
                                       @RequestParam String parentPath) {
        sysMenusCustomService.addBatch(addDTOList, parentPath);
        return success();
    }

}
